<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class article extends My_Controller {

    var $name = 'menu';
    
    public function __construct()
    {
        parent::__construct();
        //$this->load->model('menu_model');
    }
    
    public function index(){
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'article'));
        $this->template->parse_view('content', 'default/article/article_index', array());
       // Render template
        $this->template->render();
    }
    
    function list_article($page = 1){
        $this->load->model('article_model');
        
        $itemsPerPage=20;
        $space=5;
        if($page == 0){$page=1;}
        
        
        $start = ($page - 1) * $itemsPerPage;
        $items = $this->article_model->getAllListArticle($itemsPerPage,$start);
        $totalItems = $this->article_model->countAllListArticle();
        $totalPages = round($totalItems / $itemsPerPage);
        
        if( $totalItems%$itemsPerPage != 0 ){
            $totalPages = ($totalItems - ( $totalItems%$itemsPerPage ) ) /$itemsPerPage + 1;
        }
        
        $this->load->view('default/article/ajax_list_article',array(
            'items'=> $items,
            'itemsPerPage'=>$itemsPerPage,
            'page'=>$page,
            'totalPages'=>$totalPages,
            'space' => $space
        ));
    }
    
    function list_article_choose($page = 1){
        $this->load->model('article_model');
        
        $itemsPerPage=10;
        $space=5;
        if($page == 0){$page=1;}
        
        $start = ($page - 1) * $itemsPerPage;
        $items = $this->article_model->getAllListArticle($itemsPerPage,$start);
        $totalItems = $this->article_model->countAllListArticle();
        $totalPages = round($totalItems / $itemsPerPage);
        
        if( $totalItems%$itemsPerPage != 0 ){
            $totalPages = ($totalItems - ( $totalItems%$itemsPerPage ) ) /$itemsPerPage + 1;
        }
        
        $this->load->view('default/article/ajax_list_article_choose',array(
            'items'=> $items,
            'itemsPerPage'=>$itemsPerPage,
            'page'=>$page,
            'totalPages'=>$totalPages,
            'space' => $space
        ));
    }
    
    function list_category_choose($page = 1){
        $this->load->model('article_model');
        
        $itemsPerPage=20;
        $space=5;
        if($page == 0){$page=1;}
        
        
        $start = ($page - 1) * $itemsPerPage;
        $items = $this->article_model->getAllListArticle($itemsPerPage,$start);
        $totalItems = $this->article_model->countAllListArticle();
        $totalPages = round($totalItems / $itemsPerPage);
        
        if( $totalItems%$itemsPerPage != 0 ){
            $totalPages = ($totalItems - ( $totalItems%$itemsPerPage ) ) /$itemsPerPage + 1;
        }
        
        $this->load->view('default/article/ajax_list_article_choose',array(
            'items'=> $items,
            'itemsPerPage'=>$itemsPerPage,
            'page'=>$page,
            'totalPages'=>$totalPages,
            'space' => $space
        ));
    }
    
    function insert(){
        $this->load->model('article_model');
        $this->load->model('user_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('jimi_string_helper');
        $user = $this->user_model->findById($this->myauth->user_id());
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('title', 'Title', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('content', 'Content', 'require');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');

            }else{

                $title = $this->input->post('title');
                $tag = $this->input->post('tag');
                $intro = $this->input->post('intro');
                $image_path = $this->input->post('image_path');
                $content = $this->input->post('content');
                $category_id = $this->input->post('category_id');
                
                $auth_id = $user->id;
                
                $insertdata = array(
                    'title' => $title,
                    'slug' => stripUnicode($title),
                    'image_path' => $image_path,
                    'tag' => $tag,
                    'intro' => $intro,
                    'content' => $content,
                    'category_id' => $category_id,
                    'auth_id' => $auth_id,
                    'created' => date('Y-m-d H:i:s'),
                    'update' => date('Y-m-d H:i:s'),
                    'view' => 0,
                    'status' => 0
                );
                $new_articleId = $this->article_model->insertArticle($insertdata);
                
                redirect('article', 'location', 301);
            }
        }
        
        $categories = $this->article_model->getAllCategoryBySection();
        
        $data_sidebar = array(
            'menu' => 'roles'
        );
        
        $this->template->add_js('html/ckeditor/ckeditor.js');
        $this->template->add_js('html/ckfinder/ckfinder.js');
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
        $this->template->parse_view('content', 'default/article/insert_article', array(
            'action' => site_url('article/insert'),
            'page_type' => 'insert',
            'categories' => $categories
        ));
       // Render template
        $this->template->render();
    }
    
    function edit($articleId = 0){
        $this->load->model('article_model');
        $this->load->model('user_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->helper('jimi_string_helper');
        //$user = $this->user_model->findById($this->myauth->user_id());
        
        $article = $this->article_model->getArticleById($articleId);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('title', 'Title', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('content', 'Content', 'require');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');

            }else{

                $title = $this->input->post('title');
                $tag = $this->input->post('tag');
                $intro = $this->input->post('intro');
                $image_path = $this->input->post('image_path');
                $content = $this->input->post('content');
                $category_id = $this->input->post('category_id');
                
                $updatedata = array(
                    'title' => $title,
                    'slug' => stripUnicode($title),
                    'image_path' => $image_path,
                    'tag' => $tag,
                    'intro' => $intro,
                    'content' => $content,
                    'category_id' => $category_id,
                    'update' => date('Y-m-d H:i:s')
                    //'auth_id' => $auth_id,
                    //'created' => date('Y-m-d H:i:s'),
                    //'view' => 0,
                    //'status' => 0
                );
                $this->article_model->updateArticle($articleId,$updatedata);
                
                redirect('article', 'location', 301);
            }
        }
        
        $categories = $this->article_model->getAllCategoryBySection();
        
        $data_sidebar = array(
            'menu' => 'roles'
        );
        
        $this->template->add_js('html/ckeditor/ckeditor.js');
        $this->template->add_js('html/ckfinder/ckfinder.js');
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',$data_sidebar);
        $this->template->parse_view('content', 'default/article/insert_article', array(
            'action' => site_url('article/edit/'.$articleId),
            'page_type' => 'edit',
            'categories' => $categories,
            'article' => $article
        ));
       // Render template
        $this->template->render();
    }
}